import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import Industry from 'src/entities/industry.entity';
import { IsNull, Repository } from 'typeorm';
import { FindIndustryDto } from './industry.dto';

@Injectable()
export class IndustryService {
  constructor(@InjectRepository(Industry) private industryRepository: Repository<Industry>) {}

  async find(query: FindIndustryDto) {
    const { page, size, name, pid } = query;

    const queryBuilder = this.industryRepository
      .createQueryBuilder('industry')
      .select(['industry.id', 'industry.name'])
      .skip(size * (page - 1))
      .take(size);

    // if (name !== undefined) {
    //   queryBuilder.andWhere('industry.name = :name', { name });
    // }

    if (pid !== undefined) {
      queryBuilder.andWhere('industry.pid = :pid', { pid });
    }

    const [list, count] = await queryBuilder.getManyAndCount();
    return { list, count };
  }
}
